<?php

/**
 *
 * @author Administrator
 */
class CategoryController {
    // TODO - Insert your code here
    private $path;
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/view_category/temp/";
        $this->homeshow = array(1 => "Show sản phẩm ở trang chủ", 0 => "Không show sản phẩm ở trang chủ");
        $this->homeview = array(1 => "Hiển thị ở trang chủ", 0 => "Không hiển thị ở trang chủ");
        
        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * Create sub table item
     * @param unknown_type $fid
     * @param unknown_type $level
     * @return Ambigous <string, unknown_type, boolean>
     */
    private function viewSubTableItem($fid, $level) {
        
        // create xTemplate
        $template = new XTemplate( $this->path . "sub-table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['categoryShortColArray'], CATEGORIES, array(CATEGORY_FID => $fid), array( CATEGORY_POSITION => ASC));
        
        // create indent
        $indent = "";
        for($i = 0; $i <= $level; $i ++) {
            $indent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        $indent .= "-->";
        
        // assign item
        foreach ( $list as $item ) {
            $template->assign( INDENT, $indent);
            $template->assign( SUBITEM, $this->viewSubTableItem( $item[CATEGORY_ID], $level + 1));
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * view table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewTable() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        
        
        // get category type
        if (isset($_REQUEST[PARAM_TYPE]) && !empty($_REQUEST[PARAM_TYPE])){
            $type = $_REQUEST[PARAM_TYPE];
        } else {
            $type = CATEGORY_TYPE_PRODUCT;
        }
        $template->assign( TYPE, $type);
        
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['categoryShortColArray'], CATEGORIES, array(CATEGORY_FID => 0,  SQL_AND,CATEGORY_TYPE => $type ,SQL_AND, CATEGORY_LANGUAGE => $this->lan), array( CATEGORY_POSITION => ASC));
        
        // assign item
        foreach ( $list as $item ) {
            $template->assign( SUBITEM, $this->viewSubTableItem( $item[CATEGORY_ID], 0));
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        // assign total count and total page
        $count = $db->selectCountRecord( CATEGORIES, array(CATEGORY_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // assign sort title
        $template->assign( TABLETITLE, $GLOBALS['tableTitleArray'][CATEGORIES][$type]);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * Create sub table item
     * @param unknown_type $fid
     * @param unknown_type $level
     * @return Ambigous <string, unknown_type, boolean>
     */
    private function viewSubSortItem($fid) {
        
        // create xTemplate
        $template = new XTemplate( $this->path . "sub-sort.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['categoryShortColArray'], CATEGORIES, array(CATEGORY_FID => $fid), array( CATEGORY_POSITION => ASC));
        
        // assign item
        foreach ( $list as $item ) {
            $template->assign( SUBITEM, $this->viewSubSortItem( $item[CATEGORY_ID]));
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * view sort page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewSort() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/sort.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_SORT);
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        
        // get category type
        if (isset($_REQUEST[PARAM_TYPE]) && !empty($_REQUEST[PARAM_TYPE])){
            $type = $_REQUEST[PARAM_TYPE];
        } else {
            $type = CATEGORY_TYPE_PRODUCT;
        }
        $template->assign( TYPE, $type);
        
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['categoryShortColArray'], CATEGORIES, array(CATEGORY_FID => 0,  SQL_AND,CATEGORY_TYPE => $type ,SQL_AND, CATEGORY_LANGUAGE => $this->lan), array( CATEGORY_POSITION => ASC));
        
        // assign item
        foreach ( $list as $item ) {
            $template->assign( SUBITEM, $this->viewSubSortItem( $item[CATEGORY_ID]));
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        // assign total count and total page
        $count = $db->selectCountRecord( CATEGORIES, array(CATEGORY_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // assign sort title
        $template->assign( SORTTITLE, $GLOBALS['tableTitleArray'][CATEGORIES][$type]);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * view form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_CATEGORIES);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get category type
        if (isset($_REQUEST[PARAM_TYPE]) && !empty($_REQUEST[PARAM_TYPE])){
            $type = $_REQUEST[PARAM_TYPE];
        } else {
            $type = CATEGORY_TYPE_PRODUCT;
        }
        
        // get id
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            $db = new DBAccess();
            $item = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_ID => $_REQUEST[PARAM_ID]));
            $title = $GLOBALS['formTitleArray'][CATEGORIES][EDIT][$this->lan] . $item[CATEGORY_TITLE];
            
        } else {
            $item = array();
            $item[CATEGORY_FID] = $_REQUEST[PARAM_FID];
            $item[CATEGORY_TYPE] = $type;
            $item[CATEGORY_HOMESHOW] = 0;
            $item[CATEGORY_HOMEVIEW] = 0;
            $item[PRODUCT_FESH] = 0;
            $title = $GLOBALS['formTitleArray'][CATEGORIES][ADD][$this->lan];
            
        }
        
        $item[CATEGORY_HOMESHOW] = createSelectTagFormData( CATEGORY_HOMESHOW, "full-width", $this->homeshow, $item[CATEGORY_HOMESHOW]);
        $item[CATEGORY_HOMEVIEW] = createSelectTagFormData( CATEGORY_HOMEVIEW, "full-width", $this->homeview, $item[CATEGORY_HOMEVIEW]);
        
        // assign gird title
        $template->assign( FORMTITLE, $title);
        
        // assign form item
        $template->assign( ITEM, $item);
        $template->parse( PAGECONTENT . "." . ITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * sort child item
     * @param unknown_type $array
     * @param unknown_type $fid
     */
    private function sortChildItem($array, $fid) {

        $position = 1;
        $db = new DBAccess();
        
        foreach ( $array as $key => $value ) {
            $value = ( int ) ($value);
            if ($value == $fid) {
                $caterory = array(CATEGORY_ID => $key, CATEGORY_FID => $fid, CATEGORY_POSITION => $position);
                $position ++;
                $db->updateData( $caterory, CATEGORIES);
                $this->sortChildItem( $array, $key);
            }
        }
    
    }

    /**
     * submit sort function
     * @return multitype:
     */
    public function submitSort() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $array = $_POST['list'];
        
        // update position
        $db = new DBAccess();
        $position = 1;
        
        foreach ( $array as $key => $value ) {
            $value = ( int ) $value;
            if ($value == 0) {
                $caterory = array(CATEGORY_ID => $key, CATEGORY_FID => 0, CATEGORY_POSITION => $position);
                $position ++;
                $db->updateData( $caterory, CATEGORIES);
                $this->sortChildItem( $array, $key);
            }
        }
        
        $response_array[STATUS] = STATUS_OK;
        $response_array[MESSAGE] = MESSAGE_UPDATE_SUCCESSFUL;
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit form function
     * @return multitype:string
     */
    public function submitForm() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        $item[CATEGORY_ID] = $_REQUEST[CATEGORY_ID];
        
        $item[CATEGORY_TITLE] = $_REQUEST[CATEGORY_TITLE];
        $item[CATEGORY_IMAGELINK] = $_REQUEST[CATEGORY_IMAGELINK];
        $item[CATEGORY_PAGEIMAGE] = $_REQUEST[CATEGORY_PAGEIMAGE];
        
        $item[CATEGORY_FID] = $_REQUEST[CATEGORY_FID];
        $item[CATEGORY_TYPE] = $_REQUEST[CATEGORY_TYPE];
        
        $item[CATEGORY_DESCRIPTION] = $_REQUEST[CATEGORY_DESCRIPTION];
        
        $item[CATEGORY_TITLETAG] = $_REQUEST[CATEGORY_TITLETAG];
        $item[CATEGORY_URL] = encodeURL( $_REQUEST[CATEGORY_URL]);
        $item[CATEGORY_METAKEYWORD] = $_REQUEST[CATEGORY_METAKEYWORD];
        $item[CATEGORY_METADESCRIPTION] = $_REQUEST[CATEGORY_METADESCRIPTION];
        
        $item[CATEGORY_HOMESHOW] = $_REQUEST[CATEGORY_HOMESHOW];
        $item[CATEGORY_HOMEVIEW] = $_REQUEST[CATEGORY_HOMEVIEW];
        
        $item[CATEGORY_UPDATETIME] = getCurrentDateTime();
        $item[CATEGORY_LANGUAGE] = $this->lan;
        $item[CATEGORY_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[CATEGORY_TITLETAG])){
            $item[CATEGORY_TITLETAG] = $item[CATEGORY_TITLE];
        }
        if (empty($item[CATEGORY_URL])){
            $item[CATEGORY_URL] = encodeURL( $_REQUEST[CATEGORY_TITLE]);
        }
        if (empty($item[CATEGORY_METAKEYWORD])){
            $item[CATEGORY_METAKEYWORD] = $item[CATEGORY_TITLE] . "," .encodeString($item[CATEGORY_TITLE]);
        }
        if (empty($item[CATEGORY_METADESCRIPTION])){
            $item[CATEGORY_METADESCRIPTION] = $item[CATEGORY_DESCRIPTION];
        }
        
        // check url
        if (checkURLExist( CATEGORIES, $item[CATEGORY_URL], $item[CATEGORY_ID])) {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = getMessage( MESSAGE_URL_EXISTED);
            
            // return repnose array;
            return $response_array;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[CATEGORY_ID] == 0) {
            if ($db->insertData( $item, CATEGORIES)) {
                $new = $db->selectOneByCause( array(CATEGORY_ID, CATEGORY_URL), CATEGORIES, array(CATEGORY_URL => $item[CATEGORY_URL]));
                updateDataType(CATEGORIES, $new[CATEGORY_ID], $new[CATEGORY_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_CATEGORIES . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&type=" . $item[CATEGORY_TYPE];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, CATEGORIES)) {
                updateDataType(CATEGORIES, $item[CATEGORY_ID], $item[CATEGORY_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_CATEGORIES . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&type=" . $item[CATEGORY_TYPE];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            
            $db = new DBAccess();
            
            if ($db->deleteData( CATEGORIES, $_REQUEST[PARAM_ID])) {
                deleteDataType(CATEGORIES, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDelteMany() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
            
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( CATEGORIES, $id)) {
                        deleteDataType(CATEGORIES, $id);
                    } else {
                        $delete = false;
                    }
                }
                
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }
    
    /**
     * 
     * @param unknown_type $fid
     * @param unknown_type $level
     * @param unknown_type $cateArray
     */
    private function adminSubCateArray($fid, $level, $cateArray){
    
        $inden = "";
        for( $i = 0 ; $i <= $level; $i++){
            $inden .="&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        $inden.= "-> ";
    
        $db = new DBAccess();
    
        $farthers = $db->selectManyByCause(array(CATEGORY_ID, CATEGORY_URL, CATEGORY_TITLE), CATEGORIES, array(CATEGORY_FID => $fid));
    
        if ($farthers == null || count($farthers) <= 0){
            return $cateArray;
        }
    
        foreach ($farthers as $cate){
            $cateArray[$cate[CATEGORY_ID]] = $inden . $cate[CATEGORY_TITLE];
            $cateArray = $this->adminSubCateArray($cate[CATEGORY_ID], $level + 1, $cateArray);
        }
    
        return $cateArray;
    }
    
    /**
     *
     * @param unknown_type $type
     */
    public function adminCateArray($type){
    
        $cateArray = array();
        $cateArray[''] = "------------------";
        $db = new DBAccess();
    
        $farthers = $db->selectManyByCause(array(CATEGORY_ID, CATEGORY_URL, CATEGORY_TITLE), CATEGORIES, array(CATEGORY_FID => 0, SQL_AND, CATEGORY_TYPE => $type), array(CATEGORY_POSITION => ASC));
    
        foreach ($farthers as $cate){
            $cateArray[$cate[CATEGORY_ID]] = $cate[CATEGORY_TITLE];
            $cateArray = $this->adminSubCateArray($cate[CATEGORY_ID], 0, $cateArray);
        }
    
        return $cateArray;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>